{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Biome Level statistics for model: GS1_MaxScaler\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import the libraries\n",
    "import ee\n",
    "import pandas as pd\n",
    "import os\n",
    "import numpy as np\n",
    "import random\n",
    "from random import sample\n",
    "import itertools \n",
    "import geopandas as gpd\n",
    "from sklearn.metrics import r2_score\n",
    "from termcolor import colored # this is allocate colour and fonts type for the print title and text\n",
    "from IPython.display import display, HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "#check the working directory of local drive for Grid search result table loading\n",
    "# os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# initialize the earth engine API\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1 Load the required composites and images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the basic maps that needed for the analysis\n",
    "# load the carbon concentration map\n",
    "carbonConcentration = ee.Image(\"users/leonidmoore/ForestBiomass/Biome_level_Wood_Carbon_Conentration_Map\")\n",
    "# load the root shoot ratio map\n",
    "rootShootRatio = ee.Image(\"users/leonidmoore/ForestBiomass/Root_shoot_ratio_Map\").unmask()\n",
    "# load the two composites tha will be used in the analysis\n",
    "compositeImage =ee.Image(\"users/leonidmoore/ForestBiomass/20200915_Forest_Biomass_Predictors_Image\")\n",
    "compositeImageNew = ee.Image(\"projects/crowtherlab/Composite/CrowtherLab_Composite_30ArcSec\")\n",
    "# openWater map\n",
    "openWaterMask = compositeImageNew.select('ConsensusLandCoverClass_Open_Water').lte(0);\n",
    "# load the biome layer \n",
    "biomeLayer = compositeImage.select(\"WWF_Biome\")\n",
    "# define a pixel area layer with unit km2\n",
    "pixelAreaMap = ee.Image.pixelArea().divide(10000);\n",
    "# define the boundary geography reference\n",
    "unboundedGeo = ee.Geometry.Polygon([-180, 88, 0, 88, 180, 88, 180, -88, 0, -88, -180, -88], None, False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'state': 'READY',\n",
       " 'description': 'OpenWater_map_Export',\n",
       " 'creation_timestamp_ms': 1683646532274,\n",
       " 'update_timestamp_ms': 1683646532274,\n",
       " 'start_timestamp_ms': 0,\n",
       " 'task_type': 'EXPORT_IMAGE',\n",
       " 'id': 'NJ6D5REYVJA2IZKX27LVHOMW',\n",
       " 'name': 'projects/earthengine-legacy/operations/NJ6D5REYVJA2IZKX27LVHOMW'}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "openWaterExport = ee.batch.Export.image.toCloudStorage(image = openWaterMask.unmask(),\n",
    "                                                  description = 'OpenWater_map_Export',\n",
    "                                                  fileNamePrefix = 'ForestBiomassExport/Open_Water_mask_Map',\n",
    "                                                  region = unboundedGeo,\n",
    "                                                  bucket = \"crowtherlab_gcsb_lidong\",\n",
    "                                                  crs = 'EPSG:4326',\n",
    "                                                  crsTransform = [0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                                  maxPixels = 1e13,\n",
    "                                                  fileFormat = 'GeoTIFF')\n",
    "# start the export task\n",
    "openWaterExport.start()\n",
    "# show the task status\n",
    "openWaterExport.status()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 Load the biomass density maps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the carbon density layers\n",
    "potentialDensity = ee.Image(\"users/leonidmoore/ForestBiomass/GroundSourcedModel/EnsambledMaps/Predicted_GS1_MaxScaler_Potential_density_Ensambled_Mean_20230427\").unmask()\n",
    "presentDensity = ee.Image(\"users/leonidmoore/ForestBiomass/GroundSourcedModel/EnsambledMaps/Predicted_GS1_MaxScaler_Present_density_Ensambled_Mean_20230427\").unmask()\n",
    "\n",
    "# define the standard projection\n",
    "stdProj = potentialDensity.projection();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 Adjust the present and potential density maps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the present and potential forest cover\n",
    "presentForestCover = compositeImage.select('PresentTreeCover').unmask() # uniform with potential in the  0-1 scale\n",
    "potentialCoverAdjusted = ee.Image(\"users/leonidmoore/ForestBiomass/Bastin_et_al_2019_Potential_Forest_Cover_Adjusted\").unmask().rename('PotentialForestCover')\n",
    "# define the present and potential forest cover masks\n",
    "presentMask= presentForestCover.gt(0)\n",
    "potentialMask= potentialCoverAdjusted.gt(0)\n",
    "\n",
    "# check the difference of the two density maps\n",
    "potentialHigher = potentialDensity.multiply(pixelAreaMap).subtract(presentDensity.multiply(pixelAreaMap)).gte(0)\n",
    "potentialLower = potentialDensity.multiply(pixelAreaMap).subtract(presentDensity.multiply(pixelAreaMap)).lt(0)\n",
    "# replace the lower potential value by present biomass density value\n",
    "agbPotentialDensity = presentDensity.multiply(potentialLower).add(potentialDensity.multiply(potentialHigher))\n",
    "# add the root biomass to the AGB to get TGB\n",
    "tgbPotentialDensity = agbPotentialDensity.multiply(rootShootRatio).add(agbPotentialDensity)\n",
    "tgbPresentDensity = presentDensity.multiply(rootShootRatio).add(presentDensity)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4 Partioning the potential cover into different landuse types"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load all the landuse type layers\n",
    "croplandOrg = ee.Image(\"users/leonidmoore/ForestBiomass/HYDE31/cropland_Percent\").rename('cropland').divide(100).reproject(crs=stdProj);\n",
    "grazingOrg = ee.Image(\"users/leonidmoore/ForestBiomass/HYDE31/grazing_Percent\").rename('grazing').divide(100).reproject(crs=stdProj);\n",
    "pastureOrg = ee.Image(\"users/leonidmoore/ForestBiomass/HYDE31/pasture_Percent\").rename('pasture').divide(100).reproject(crs=stdProj);\n",
    "rangelandOrg = ee.Image(\"users/leonidmoore/ForestBiomass/HYDE31/rangeland_Percent\").rename('rangeland').divide(100).reproject(crs=stdProj);\n",
    "urbanOrg = compositeImage.select(['LandCoverClass_Urban_Builtup']).divide(100).unmask().reproject(crs=stdProj);\n",
    "snowIceOrg = compositeImageNew.select(['ConsensusLandCoverClass_Snow_Ice']).divide(100).unmask().reproject(crs=stdProj);\n",
    "openWaterOrg = compositeImageNew.select(['ConsensusLandCoverClass_Open_Water']).divide(100).unmask().reproject(crs=stdProj);\n",
    "# define the total landcover types\n",
    "sumCover = presentForestCover.add(pastureOrg).add(rangelandOrg).add(croplandOrg).add(urbanOrg).add(openWaterOrg).add(snowIceOrg);\n",
    "oneSubtract = ee.Image(1).subtract(sumCover);\n",
    "freeland = oneSubtract.multiply(oneSubtract.gte(0));\n",
    "# get the scale ratio for pixels with sumCover larger than 1\n",
    "scaleRatio = ee.Image(1).subtract(presentForestCover).divide(sumCover.subtract(presentForestCover)).multiply(oneSubtract.lt(0));\n",
    "# get the ratio of these three disturbed maps\n",
    "pasture = pastureOrg.multiply(scaleRatio).multiply(oneSubtract.lt(0)).add(pastureOrg.multiply(oneSubtract.gte(0))).unmask();\n",
    "rangeland = rangelandOrg.multiply(scaleRatio).multiply(oneSubtract.lt(0)).add(rangelandOrg.multiply(oneSubtract.gte(0))).unmask();\n",
    "cropland = croplandOrg.multiply(scaleRatio).multiply(oneSubtract.lt(0)).add(croplandOrg.multiply(oneSubtract.gte(0))).unmask();\n",
    "urban = urbanOrg.multiply(scaleRatio).multiply(oneSubtract.lt(0)).add(urbanOrg.multiply(oneSubtract.gte(0))).unmask();\n",
    "openWater = openWaterOrg.multiply(scaleRatio).multiply(oneSubtract.lt(0)).add(openWaterOrg.multiply(oneSubtract.gte(0))).unmask();\n",
    "snowIce = snowIceOrg.multiply(scaleRatio).multiply(oneSubtract.lt(0)).add(snowIceOrg.multiply(oneSubtract.gte(0))).unmask();\n",
    "sumTT = presentForestCover.add(pasture).add(rangeland).add(cropland).add(urban).add(freeland).add(openWater).add(snowIce).unmask();\n",
    "\n",
    "effectivePotentialMask = freeland.add(rangeland).add(pasture).add(cropland).add(urban).gt(0);\n",
    "# there are some pixels without any landcover survived but with open water and ice and snow. here we mask these pixels out\n",
    "sumlandCover = pastureOrg.add(rangelandOrg).add(croplandOrg).add(urbanOrg).add(freeland);\n",
    "restorationMap = potentialCoverAdjusted.subtract(presentForestCover).mask(effectivePotentialMask).unmask();\n",
    "\n",
    "# sum all these scaled layersv\n",
    "scaledSum = pasture.add(rangeland).add(cropland).add(urban).add(freeland);\n",
    "potentialCoverFinal = restorationMap.add(presentForestCover);\n",
    "# allocate the potential equally to each layer\n",
    "freelandPotentialCover = freeland.divide(scaledSum).multiply(restorationMap).unmask();\n",
    "rangelandPotentialCover = rangeland.divide(scaledSum).multiply(restorationMap).unmask();\n",
    "pasturePotentialCover = pasture.divide(scaledSum).multiply(restorationMap).unmask();\n",
    "croplandPotentialCover = cropland.divide(scaledSum).multiply(restorationMap).unmask();\n",
    "urbanPotentialCover = urban.divide(scaledSum).multiply(restorationMap).unmask();\n",
    "#  allocate the freeland potential in pixels with forest cover larger than 10% to conservation potential\n",
    "freelandForConsevation = freelandPotentialCover.multiply(presentForestCover.gte(0.1)).unmask();\n",
    "maximumPotentialCover = freelandForConsevation.add(presentForestCover);\n",
    "# calucate the reall freeland outside of forest\n",
    "freelandLeftMap = freelandPotentialCover.subtract(freelandForConsevation).unmask()# the left positive pixels are real freeland pixels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5 Partioning the biomass potential into different landuse types"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# calculate the existing carbon, present potential carbon and absolute potential carbon in forests\n",
    "absoluteImage1 = tgbPresentDensity.multiply(pixelAreaMap).multiply(presentMask).divide(1000000000).rename('Present')\n",
    "absoluteImage3 = tgbPotentialDensity.multiply(pixelAreaMap).multiply(potentialCoverFinal.gt(0)).divide(1000000000).rename('AbsolutePotential')\n",
    "# get the sum of the potential covers\n",
    "potentialCoverSum = freelandLeftMap.add(rangelandPotentialCover).add(pasturePotentialCover).add(croplandPotentialCover).add(urbanPotentialCover)\n",
    "\n",
    "trueRestorationPotential = absoluteImage3.subtract(absoluteImage1).multiply(1000000000)\n",
    "ratioPotentialBiomassDensity = absoluteImage1.multiply(potentialCoverFinal.divide(presentForestCover))\n",
    "#  get the real for conservation potential\n",
    "realDensityIncreased = absoluteImage3.subtract(absoluteImage1).mask(absoluteImage3.subtract(ratioPotentialBiomassDensity).gt(0)).unmask()\n",
    "realDensityNotIncreased = absoluteImage3.subtract(absoluteImage1).mask(absoluteImage3.subtract(ratioPotentialBiomassDensity).lte(0)).unmask()\n",
    "trueReforestationPotential = realDensityNotIncreased.add(realDensityIncreased.multiply(ee.Image(1).subtract(presentForestCover.divide(potentialCoverFinal))))\n",
    "\n",
    "conservationPotentialPart1 = realDensityIncreased.multiply(presentForestCover.add(freelandForConsevation).divide(potentialCoverFinal))\n",
    "conservationPotentialPart2 = realDensityNotIncreased.multiply(freelandForConsevation.divide(potentialCoverFinal.subtract(presentForestCover)))\n",
    "# calculate the part of the potential inside the forest cover which was allocate to conservation potential.\n",
    "freelandForConservation1 = realDensityIncreased.multiply(freelandForConsevation.divide(potentialCoverFinal))\n",
    "freelandForConservation = freelandForConservation1.add(conservationPotentialPart2).rename('FreeToConservation')\n",
    "\n",
    "absoluteImage2 = conservationPotentialPart1.add(conservationPotentialPart2).add(absoluteImage1).rename('PresentPotential')\n",
    "\n",
    "trueReforestationPotential = absoluteImage3.subtract(absoluteImage2)\n",
    "\n",
    "absoluteImage4 = trueReforestationPotential.multiply(freelandLeftMap.divide(potentialCoverSum)).rename('FreelandPotential')\n",
    "absoluteImage5 = trueReforestationPotential.multiply(rangelandPotentialCover.divide(potentialCoverSum)).rename('RangelandPotential')\n",
    "absoluteImage6 = trueReforestationPotential.multiply(pasturePotentialCover.divide(potentialCoverSum)).rename('PasturePotential')\n",
    "absoluteImage7 = trueReforestationPotential.multiply(croplandPotentialCover.divide(potentialCoverSum)).rename('CroplandPotential')\n",
    "absoluteImage8 = trueReforestationPotential.multiply(urbanPotentialCover.divide(potentialCoverSum)).rename('UrbanPotential')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculate the potential numbers and write into local folder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m\u001b[34mThe biomass partition results in biome: \n",
      "\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Present</th>\n",
       "      <th>PresentPotential</th>\n",
       "      <th>AbsolutePotential</th>\n",
       "      <th>FreelandPotential</th>\n",
       "      <th>RangelandPotential</th>\n",
       "      <th>PasturePotential</th>\n",
       "      <th>CroplandPotential</th>\n",
       "      <th>UrbanPotential</th>\n",
       "      <th>FreeToConservation</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>211.043662</td>\n",
       "      <td>244.922866</td>\n",
       "      <td>264.650285</td>\n",
       "      <td>3.567926</td>\n",
       "      <td>0.058737</td>\n",
       "      <td>7.063837</td>\n",
       "      <td>8.798370</td>\n",
       "      <td>0.238548</td>\n",
       "      <td>7.085752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5.942344</td>\n",
       "      <td>8.766004</td>\n",
       "      <td>13.429488</td>\n",
       "      <td>1.102579</td>\n",
       "      <td>0.011769</td>\n",
       "      <td>1.368624</td>\n",
       "      <td>2.155628</td>\n",
       "      <td>0.024884</td>\n",
       "      <td>0.681798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.288131</td>\n",
       "      <td>3.556911</td>\n",
       "      <td>4.254547</td>\n",
       "      <td>0.227330</td>\n",
       "      <td>0.008612</td>\n",
       "      <td>0.262212</td>\n",
       "      <td>0.197168</td>\n",
       "      <td>0.002313</td>\n",
       "      <td>0.331992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>32.903483</td>\n",
       "      <td>43.411064</td>\n",
       "      <td>55.742646</td>\n",
       "      <td>2.806932</td>\n",
       "      <td>0.013669</td>\n",
       "      <td>3.687599</td>\n",
       "      <td>5.464219</td>\n",
       "      <td>0.359164</td>\n",
       "      <td>3.125498</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>14.902299</td>\n",
       "      <td>19.200447</td>\n",
       "      <td>21.026061</td>\n",
       "      <td>0.824180</td>\n",
       "      <td>0.020183</td>\n",
       "      <td>0.545206</td>\n",
       "      <td>0.401844</td>\n",
       "      <td>0.034200</td>\n",
       "      <td>1.685827</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>21.211961</td>\n",
       "      <td>24.172654</td>\n",
       "      <td>25.677657</td>\n",
       "      <td>1.237901</td>\n",
       "      <td>0.000573</td>\n",
       "      <td>0.125762</td>\n",
       "      <td>0.132128</td>\n",
       "      <td>0.008640</td>\n",
       "      <td>2.137828</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>32.256835</td>\n",
       "      <td>47.568064</td>\n",
       "      <td>66.047567</td>\n",
       "      <td>3.210563</td>\n",
       "      <td>6.358823</td>\n",
       "      <td>5.370555</td>\n",
       "      <td>3.498253</td>\n",
       "      <td>0.041309</td>\n",
       "      <td>3.448819</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2.498406</td>\n",
       "      <td>3.163184</td>\n",
       "      <td>10.381984</td>\n",
       "      <td>1.296335</td>\n",
       "      <td>2.027124</td>\n",
       "      <td>1.040650</td>\n",
       "      <td>2.810176</td>\n",
       "      <td>0.044514</td>\n",
       "      <td>0.179690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1.159241</td>\n",
       "      <td>1.661545</td>\n",
       "      <td>2.287263</td>\n",
       "      <td>0.161046</td>\n",
       "      <td>0.246571</td>\n",
       "      <td>0.137185</td>\n",
       "      <td>0.075920</td>\n",
       "      <td>0.004996</td>\n",
       "      <td>0.109674</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2.476383</td>\n",
       "      <td>3.594589</td>\n",
       "      <td>5.354465</td>\n",
       "      <td>0.432442</td>\n",
       "      <td>0.590884</td>\n",
       "      <td>0.361872</td>\n",
       "      <td>0.371217</td>\n",
       "      <td>0.003461</td>\n",
       "      <td>0.218131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2.470093</td>\n",
       "      <td>2.714619</td>\n",
       "      <td>3.770694</td>\n",
       "      <td>1.052223</td>\n",
       "      <td>0.000761</td>\n",
       "      <td>0.000248</td>\n",
       "      <td>0.002673</td>\n",
       "      <td>0.000170</td>\n",
       "      <td>0.176988</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1.640186</td>\n",
       "      <td>2.376552</td>\n",
       "      <td>4.262632</td>\n",
       "      <td>0.533464</td>\n",
       "      <td>0.004731</td>\n",
       "      <td>0.613836</td>\n",
       "      <td>0.706804</td>\n",
       "      <td>0.027246</td>\n",
       "      <td>0.224021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1.396874</td>\n",
       "      <td>2.109143</td>\n",
       "      <td>7.416946</td>\n",
       "      <td>2.197810</td>\n",
       "      <td>2.043380</td>\n",
       "      <td>0.356640</td>\n",
       "      <td>0.693887</td>\n",
       "      <td>0.016086</td>\n",
       "      <td>0.184101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1.955499</td>\n",
       "      <td>2.502592</td>\n",
       "      <td>2.832839</td>\n",
       "      <td>0.111218</td>\n",
       "      <td>0.000472</td>\n",
       "      <td>0.064140</td>\n",
       "      <td>0.145184</td>\n",
       "      <td>0.009234</td>\n",
       "      <td>0.140606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sum</th>\n",
       "      <td>334.145397</td>\n",
       "      <td>409.720233</td>\n",
       "      <td>487.135076</td>\n",
       "      <td>18.761950</td>\n",
       "      <td>11.386290</td>\n",
       "      <td>20.998366</td>\n",
       "      <td>25.453471</td>\n",
       "      <td>0.814765</td>\n",
       "      <td>19.730723</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Present  PresentPotential  AbsolutePotential  FreelandPotential  \\\n",
       "0    211.043662        244.922866         264.650285           3.567926   \n",
       "1      5.942344          8.766004          13.429488           1.102579   \n",
       "2      2.288131          3.556911           4.254547           0.227330   \n",
       "3     32.903483         43.411064          55.742646           2.806932   \n",
       "4     14.902299         19.200447          21.026061           0.824180   \n",
       "5     21.211961         24.172654          25.677657           1.237901   \n",
       "6     32.256835         47.568064          66.047567           3.210563   \n",
       "7      2.498406          3.163184          10.381984           1.296335   \n",
       "8      1.159241          1.661545           2.287263           0.161046   \n",
       "9      2.476383          3.594589           5.354465           0.432442   \n",
       "10     2.470093          2.714619           3.770694           1.052223   \n",
       "11     1.640186          2.376552           4.262632           0.533464   \n",
       "12     1.396874          2.109143           7.416946           2.197810   \n",
       "13     1.955499          2.502592           2.832839           0.111218   \n",
       "sum  334.145397        409.720233         487.135076          18.761950   \n",
       "\n",
       "     RangelandPotential  PasturePotential  CroplandPotential  UrbanPotential  \\\n",
       "0              0.058737          7.063837           8.798370        0.238548   \n",
       "1              0.011769          1.368624           2.155628        0.024884   \n",
       "2              0.008612          0.262212           0.197168        0.002313   \n",
       "3              0.013669          3.687599           5.464219        0.359164   \n",
       "4              0.020183          0.545206           0.401844        0.034200   \n",
       "5              0.000573          0.125762           0.132128        0.008640   \n",
       "6              6.358823          5.370555           3.498253        0.041309   \n",
       "7              2.027124          1.040650           2.810176        0.044514   \n",
       "8              0.246571          0.137185           0.075920        0.004996   \n",
       "9              0.590884          0.361872           0.371217        0.003461   \n",
       "10             0.000761          0.000248           0.002673        0.000170   \n",
       "11             0.004731          0.613836           0.706804        0.027246   \n",
       "12             2.043380          0.356640           0.693887        0.016086   \n",
       "13             0.000472          0.064140           0.145184        0.009234   \n",
       "sum           11.386290         20.998366          25.453471        0.814765   \n",
       "\n",
       "     FreeToConservation  \n",
       "0              7.085752  \n",
       "1              0.681798  \n",
       "2              0.331992  \n",
       "3              3.125498  \n",
       "4              1.685827  \n",
       "5              2.137828  \n",
       "6              3.448819  \n",
       "7              0.179690  \n",
       "8              0.109674  \n",
       "9              0.218131  \n",
       "10             0.176988  \n",
       "11             0.224021  \n",
       "12             0.184101  \n",
       "13             0.140606  \n",
       "sum           19.730723  "
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Stack the absolute biomass layers into an Image.\n",
    "absPotentialImage = absoluteImage1.addBands(absoluteImage2).addBands(absoluteImage3).addBands(absoluteImage4).addBands(absoluteImage5).addBands(absoluteImage6).addBands(absoluteImage7).addBands(absoluteImage8).addBands(freelandForConservation)#.multiply(openWaterMask)\n",
    "# define the function to do the biome level statistics which could be applied by map      \n",
    "def biomeLevelStat(biome):\n",
    "    biomeMask = biomeLayer.eq(ee.Number(biome))\n",
    "    masked_img = absPotentialImage.mask(biomeMask)\n",
    "    output = masked_img.reduceRegion(reducer= ee.Reducer.sum(),\n",
    "                                     geometry= unboundedGeo,\n",
    "                                     crs='EPSG:4326',\n",
    "                                     crsTransform=[0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                     maxPixels= 1e13)\n",
    "    return output#.getInfo().get('Present')\n",
    "\n",
    "\n",
    "biomeList = ee.List([1,2,3,4,5,6,7,8,9,10,11,12,13,14])\n",
    "statisticTable = biomeList.map(biomeLevelStat).getInfo()\n",
    "# transform into data frame\n",
    "outputTable = pd.DataFrame(statisticTable,columns =['Present','PresentPotential','AbsolutePotential','FreelandPotential','RangelandPotential','PasturePotential','CroplandPotential','UrbanPotential','FreeToConservation'])#.round(1)\n",
    "outputTable.loc['sum'] = outputTable.sum() \n",
    "outputTable.to_csv('Data/BiomeLevelStatistics/StatisticsForModels/GS1_MaxScaler_Biome_Level_Statistics.csv',header=True,mode='w+')\n",
    "print(colored('The biomass partition results in biome: \\n', 'blue', attrs=['bold']))\n",
    "outputTable.head(15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# If you got the error 'EEException: Too many concurrent aggregations.', please re-run this chunck of code again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1m\u001b[34mThe biomass partition results in biome: \n",
      "\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Present</th>\n",
       "      <th>PresentPotential</th>\n",
       "      <th>AbsolutePotential</th>\n",
       "      <th>FreelandPotential</th>\n",
       "      <th>RangelandPotential</th>\n",
       "      <th>PasturePotential</th>\n",
       "      <th>CroplandPotential</th>\n",
       "      <th>UrbanPotential</th>\n",
       "      <th>FreeToConservation</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>257.481342</td>\n",
       "      <td>298.816944</td>\n",
       "      <td>322.885986</td>\n",
       "      <td>4.353176</td>\n",
       "      <td>0.071766</td>\n",
       "      <td>8.618430</td>\n",
       "      <td>10.734640</td>\n",
       "      <td>0.291031</td>\n",
       "      <td>8.645192</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7.249642</td>\n",
       "      <td>10.694491</td>\n",
       "      <td>16.383972</td>\n",
       "      <td>1.345146</td>\n",
       "      <td>0.014397</td>\n",
       "      <td>1.669731</td>\n",
       "      <td>2.629849</td>\n",
       "      <td>0.030358</td>\n",
       "      <td>0.831788</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.792059</td>\n",
       "      <td>4.340475</td>\n",
       "      <td>5.191756</td>\n",
       "      <td>0.277418</td>\n",
       "      <td>0.010496</td>\n",
       "      <td>0.319896</td>\n",
       "      <td>0.240647</td>\n",
       "      <td>0.002823</td>\n",
       "      <td>0.405222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>43.758419</td>\n",
       "      <td>57.731440</td>\n",
       "      <td>74.130743</td>\n",
       "      <td>3.732540</td>\n",
       "      <td>0.018158</td>\n",
       "      <td>4.904157</td>\n",
       "      <td>7.266809</td>\n",
       "      <td>0.477640</td>\n",
       "      <td>4.156166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>19.819178</td>\n",
       "      <td>25.534623</td>\n",
       "      <td>27.961420</td>\n",
       "      <td>1.095495</td>\n",
       "      <td>0.026707</td>\n",
       "      <td>0.724925</td>\n",
       "      <td>0.534202</td>\n",
       "      <td>0.045468</td>\n",
       "      <td>2.241704</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>38.159755</td>\n",
       "      <td>43.486007</td>\n",
       "      <td>46.192289</td>\n",
       "      <td>2.227109</td>\n",
       "      <td>0.000970</td>\n",
       "      <td>0.225910</td>\n",
       "      <td>0.236779</td>\n",
       "      <td>0.015515</td>\n",
       "      <td>3.846217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>39.353621</td>\n",
       "      <td>58.033493</td>\n",
       "      <td>80.578814</td>\n",
       "      <td>3.916927</td>\n",
       "      <td>7.757913</td>\n",
       "      <td>6.552196</td>\n",
       "      <td>4.267886</td>\n",
       "      <td>0.050399</td>\n",
       "      <td>4.207553</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>3.323223</td>\n",
       "      <td>4.207422</td>\n",
       "      <td>13.807971</td>\n",
       "      <td>1.724044</td>\n",
       "      <td>2.695764</td>\n",
       "      <td>1.384006</td>\n",
       "      <td>3.737534</td>\n",
       "      <td>0.059200</td>\n",
       "      <td>0.239022</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1.414526</td>\n",
       "      <td>2.027399</td>\n",
       "      <td>2.790872</td>\n",
       "      <td>0.196503</td>\n",
       "      <td>0.300819</td>\n",
       "      <td>0.167390</td>\n",
       "      <td>0.092660</td>\n",
       "      <td>0.006100</td>\n",
       "      <td>0.133830</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>3.285372</td>\n",
       "      <td>4.770504</td>\n",
       "      <td>7.109380</td>\n",
       "      <td>0.574666</td>\n",
       "      <td>0.785415</td>\n",
       "      <td>0.480866</td>\n",
       "      <td>0.493329</td>\n",
       "      <td>0.004600</td>\n",
       "      <td>0.289733</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>4.443381</td>\n",
       "      <td>4.883110</td>\n",
       "      <td>6.783638</td>\n",
       "      <td>1.893603</td>\n",
       "      <td>0.001367</td>\n",
       "      <td>0.000445</td>\n",
       "      <td>0.004808</td>\n",
       "      <td>0.000305</td>\n",
       "      <td>0.318424</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1.985335</td>\n",
       "      <td>2.876768</td>\n",
       "      <td>5.159367</td>\n",
       "      <td>0.645604</td>\n",
       "      <td>0.005746</td>\n",
       "      <td>0.742888</td>\n",
       "      <td>0.855384</td>\n",
       "      <td>0.032976</td>\n",
       "      <td>0.271168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1.690562</td>\n",
       "      <td>2.552599</td>\n",
       "      <td>8.975990</td>\n",
       "      <td>2.659709</td>\n",
       "      <td>2.472806</td>\n",
       "      <td>0.431681</td>\n",
       "      <td>0.839729</td>\n",
       "      <td>0.019467</td>\n",
       "      <td>0.222831</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2.385724</td>\n",
       "      <td>3.053182</td>\n",
       "      <td>3.456091</td>\n",
       "      <td>0.135684</td>\n",
       "      <td>0.000575</td>\n",
       "      <td>0.078260</td>\n",
       "      <td>0.177124</td>\n",
       "      <td>0.011266</td>\n",
       "      <td>0.171538</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>sum</th>\n",
       "      <td>427.142139</td>\n",
       "      <td>523.008455</td>\n",
       "      <td>621.408290</td>\n",
       "      <td>24.777624</td>\n",
       "      <td>14.162899</td>\n",
       "      <td>26.300782</td>\n",
       "      <td>32.111382</td>\n",
       "      <td>1.047148</td>\n",
       "      <td>25.980388</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Present  PresentPotential  AbsolutePotential  FreelandPotential  \\\n",
       "0    257.481342        298.816944         322.885986           4.353176   \n",
       "1      7.249642         10.694491          16.383972           1.345146   \n",
       "2      2.792059          4.340475           5.191756           0.277418   \n",
       "3     43.758419         57.731440          74.130743           3.732540   \n",
       "4     19.819178         25.534623          27.961420           1.095495   \n",
       "5     38.159755         43.486007          46.192289           2.227109   \n",
       "6     39.353621         58.033493          80.578814           3.916927   \n",
       "7      3.323223          4.207422          13.807971           1.724044   \n",
       "8      1.414526          2.027399           2.790872           0.196503   \n",
       "9      3.285372          4.770504           7.109380           0.574666   \n",
       "10     4.443381          4.883110           6.783638           1.893603   \n",
       "11     1.985335          2.876768           5.159367           0.645604   \n",
       "12     1.690562          2.552599           8.975990           2.659709   \n",
       "13     2.385724          3.053182           3.456091           0.135684   \n",
       "sum  427.142139        523.008455         621.408290          24.777624   \n",
       "\n",
       "     RangelandPotential  PasturePotential  CroplandPotential  UrbanPotential  \\\n",
       "0              0.071766          8.618430          10.734640        0.291031   \n",
       "1              0.014397          1.669731           2.629849        0.030358   \n",
       "2              0.010496          0.319896           0.240647        0.002823   \n",
       "3              0.018158          4.904157           7.266809        0.477640   \n",
       "4              0.026707          0.724925           0.534202        0.045468   \n",
       "5              0.000970          0.225910           0.236779        0.015515   \n",
       "6              7.757913          6.552196           4.267886        0.050399   \n",
       "7              2.695764          1.384006           3.737534        0.059200   \n",
       "8              0.300819          0.167390           0.092660        0.006100   \n",
       "9              0.785415          0.480866           0.493329        0.004600   \n",
       "10             0.001367          0.000445           0.004808        0.000305   \n",
       "11             0.005746          0.742888           0.855384        0.032976   \n",
       "12             2.472806          0.431681           0.839729        0.019467   \n",
       "13             0.000575          0.078260           0.177124        0.011266   \n",
       "sum           14.162899         26.300782          32.111382        1.047148   \n",
       "\n",
       "     FreeToConservation  \n",
       "0              8.645192  \n",
       "1              0.831788  \n",
       "2              0.405222  \n",
       "3              4.156166  \n",
       "4              2.241704  \n",
       "5              3.846217  \n",
       "6              4.207553  \n",
       "7              0.239022  \n",
       "8              0.133830  \n",
       "9              0.289733  \n",
       "10             0.318424  \n",
       "11             0.271168  \n",
       "12             0.222831  \n",
       "13             0.171538  \n",
       "sum           25.980388  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "deadWoodLitterRatio = ee.Image(\"users/leonidmoore/ForestBiomass/DeadWoodLitter/DeadWood_Litter_Ratio_Map\").unmask()\n",
    "\n",
    "# Stack the absolute biomass layers into an Image.\n",
    "absPotentialImage = absoluteImage1.addBands(absoluteImage2).addBands(absoluteImage3).addBands(absoluteImage4).addBands(absoluteImage5).addBands(absoluteImage6).addBands(absoluteImage7).addBands(absoluteImage8).addBands(freelandForConservation)\n",
    "# define the function to do the biome level statistics which could be applied by map      \n",
    "def biomeLevelStat(biome):\n",
    "    biomeMask = biomeLayer.eq(ee.Number(biome))\n",
    "    masked_img = absPotentialImage.mask(biomeMask).multiply(deadWoodLitterRatio)\n",
    "    output = masked_img.reduceRegion(reducer= ee.Reducer.sum(),\n",
    "                                     geometry= unboundedGeo,\n",
    "                                     crs='EPSG:4326',\n",
    "                                     crsTransform=[0.008333333333333333,0,-180,0,-0.008333333333333333,90],\n",
    "                                     maxPixels= 1e13)\n",
    "    return output#.getInfo().get('Present')\n",
    "\n",
    "\n",
    "biomeList = ee.List([1,2,3,4,5,6,7,8,9,10,11,12,13,14])\n",
    "statisticTable = biomeList.map(biomeLevelStat).getInfo()\n",
    "# transform into data frame\n",
    "outputTable = pd.DataFrame(statisticTable,columns =['Present','PresentPotential','AbsolutePotential','FreelandPotential','RangelandPotential','PasturePotential','CroplandPotential','UrbanPotential','FreeToConservation'])#.round(1)\n",
    "outputTable.loc['sum'] = outputTable.sum() \n",
    "outputTable.to_csv('Data/BiomeLevelStatistics/StatisticsForModels/GS1_MaxScaler_Biome_Level_Statistics_with_Litter.csv',header=True,mode='w+')\n",
    "print(colored('The biomass partition results in biome: \\n', 'blue', attrs=['bold']))\n",
    "outputTable.head(15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# If you got the error 'EEException: Too many concurrent aggregations.', please re-run this chunck of code again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
